<?php session_start();
if(isset($_SESSION['name'])) {
    header('location:index.php');
}?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Register</title>
    <link href="css/style.css" type="text/css" rel="stylesheet" />
    <link href="css/register.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script src="js/register.js"></script>
</head>
<body>
<?php require_once '_header.php' ?>
<?php
    if(isset($_POST['submit'])) {
        require_once 'model/database.php';
        $connect = connectDB();
        $email = mysqli_real_escape_string($connect, $_POST['email']);
        $query =    "SELECT id
                    FROM members
                    WHERE email='$email'
                    LIMIT 1";
        $result = mysqli_query($connect, $query) or die(mysqli_error($connect));
        if(mysqli_num_rows($result) == 1) {
            $error = false;
        } else {
            $name = mysqli_real_escape_string($connect,$_POST['name']);
            $passwd = mysqli_real_escape_string($connect,$_POST['password1']);
            $address = mysqli_real_escape_string($connect,$_POST['address']);
            $birthday = mysqli_real_escape_string($connect,$_POST['birthday']);
            $hash_pw = sha1($passwd);
            $query = "INSERT INTO members(email, passwd, name, birthday, address) 
                        VALUES('$email', '$hash_pw', '$name', '$birthday', '$address')";
            mysqli_query($connect, $query) or die(mysqli_error($connect));
            mkdir( "media/".$email, 0777 );
            $query = "SELECT id FROM members WHERE email='$email' LIMIT 1";
            $result = mysqli_query($connect, $query) or die(mysqli_error($connect));
            $r = mysqli_fetch_array($result, MYSQLI_ASSOC);
            $_SESSION['id'] = $r['id'];
            $_SESSION['email'] = $email;
            header('location:index.php');
        }
    }
?>
<div id="wrapper">
    <h1>REGISTER</h1>
    <form action="" id="register" method="post" name="login">
        <fieldset>
            <fieldset>
                <label for="name">Full name:</label>
                <input type="text" name="name" /> 

            </fieldset>
            <fieldset>
                <label for="email">Email:</label>
                <input type="email" name="email"/>
                <?php
                if(isset($error) && error){
                    echo '<label for="name" class="error">tai khoan da co nguoi su dung</label>';
                }
                ?>
            </fieldset>
            <fieldset>    
                <label for="password1">Password:</label>
                <input id="pass1" type="password" name="password1"/>
            </fieldset>
            <fieldset>   
                <label for="password2">Confirm password:</label>
                <input id="pass2" type="password" name="password2"/>
            </fieldset>
            <fieldset>   
                <label for="birthday">Date of birth (yyyy-mm-dd):</label>
                <input type="date" name="birthday" />
            </fieldset>
            <fieldset>   
                <label for="address">Place of birth:</label>
                <input type="text" name="address" />
            </fieldset>
            <button type="submit" name="submit">submit</button>
        </fieldset>
    </form>
</div>
<?php require_once '_footer.php' ?>
</body>
</html>